package io.renren.modules.quickService.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.modules.quickService.service.AreacodeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.quickService.entity.UserEntity;
import io.renren.modules.quickService.service.UserService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 用户
 *
 * @author zenger380
 * @email zenger380@163.com
 * @date 2018-10-08 17:19:58
 */
@RestController
@RequestMapping("quickService/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AreacodeService areacodeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("quickService:user:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("quickService:user:info")
    public R info(@PathVariable("userId") Long userId){
			UserEntity user = userService.selectById(userId);
            String[]  adminCodes =  new  String[3];
            if(user.getAdminCode() !=null){
                adminCodes =  areacodeService.getCascadeAreaByLastAdminCode(user.getAdminCode());
            }
        return R.ok().put("user", user).put("adminCodes",adminCodes);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("quickService:user:save")
    public R save(@RequestBody UserEntity user){
            user.setAddress(areacodeService.getAddressByLastAdminCode(user.getAdminCode()));
			userService.insert(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("quickService:user:update")
    public R update(@RequestBody UserEntity user){
            user.setAddress(areacodeService.getAddressByLastAdminCode(user.getAdminCode()));
			userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("quickService:user:delete")
    public R delete(@RequestBody Long[] userIds){
			userService.deleteBatchIds(Arrays.asList(userIds));

        return R.ok();
    }

}
